﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DragDraw.Service;

namespace DragDraw.Web
{
    public partial class AdminPage : System.Web.UI.Page
    {
        List<WidgetLibInfo> m_widgetLibs;
        private ArrayList ListToArrayList(List<WidgetLibInfo> libs)
        {
            ArrayList list = new ArrayList();
            foreach (WidgetLibInfo wli in libs)
            {
                LibInfo lib = new LibInfo()
                {
                    ID = wli.ID,
                    WidgetAssemblyName = wli.WidgetAssemblyName,
                    WidgetFileName = wli.WidgetFileName,
                    WidgetName = wli.WidgetName,
                    Description = wli.Description,
                    ImageUrl = wli.ImageUrl,
                    Tags = wli.Tags
                };
                list.Add(lib);
            }
            return list;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                GridViewWidget.DataSource = ListToArrayList(WidgetLibInfoData.Query());
                GridViewWidget.DataBind();
            }
            catch(Exception ex)
            {
                LabelFinalMessage.Text = ex.Message;
            }
        }

        protected void GridViewWidget_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewWidget.EditIndex = GridViewWidget.SelectedIndex;
            DataRowView wli = (DataRowView)GridViewWidget.SelectedRow.DataItem;
            TextBoxID.Text = wli["ID"].ToString();
            TextBoxDescription.Text = wli["Description"].ToString();
            TextBoxImageUrl.Text = wli["ImageUrl"].ToString();
            TextBoxTags.Text = wli["Tags"].ToString();
            TextBoxWidgetAssemblyName.Text = wli["WidgetAssemblyName"].ToString();
            TextBoxWidgetFileName.Text = wli["WidgetFileName"].ToString();
            TextBoxWidgetName.Text = wli["WidgetName"].ToString();
        }

        protected void ButtonAdd_OnClick(object sender, EventArgs e)
        {
            WidgetLibInfo lib = new WidgetLibInfo();
            lib.WidgetName = TextBoxWidgetName.Text;
            lib.WidgetFileName = TextBoxWidgetFileName.Text;
            lib.WidgetAssemblyName = TextBoxWidgetAssemblyName.Text;
            lib.ImageUrl = TextBoxImageUrl.Text;
            lib.Tags = TextBoxTags.Text;
            lib.Description = TextBoxDescription.Text;
            try
            {
                WidgetLibInfoData.Insert(lib);
                LabelFinalMessage.Text = "注册小工具库成功";
                GridViewWidget.DataSource = ListToArrayList(WidgetLibInfoData.Query());
                GridViewWidget.DataBind();
            }
            catch(DataNotFoundException ex)
            {
                LabelFinalMessage.Text = ex.Message;
            }
            catch (Exception exception)
            {
                LabelFinalMessage.Text = exception.Message;
            }
        }

        protected void ButtonDel_OnClick(object sender, EventArgs e)
        {
            try
            {
                List<WidgetLibInfo> libs = WidgetLibInfoData.Query();
                foreach (WidgetLibInfo wli in libs)
                {
                    WidgetLibInfoData.Delete(wli.ID);
                }
                LabelFinalMessage.Text = "清空小工具库成功";
                GridViewWidget.DataSource = null;
                GridViewWidget.DataBind();
            }
            catch (DataNotFoundException ex)
            {
                LabelFinalMessage.Text = ex.Message;
            }
            catch (Exception exception)
            {
                LabelFinalMessage.Text = "清空小工具库失败";
            }
        }

        protected void ButtonModify_OnClick(object sender, EventArgs e)
        {
            WidgetLibInfo lib = new WidgetLibInfo();
            lib.WidgetName = TextBoxWidgetName.Text;
            lib.WidgetFileName = TextBoxWidgetFileName.Text;
            lib.WidgetAssemblyName = TextBoxWidgetAssemblyName.Text;
            lib.ImageUrl = TextBoxImageUrl.Text;
            lib.Tags = TextBoxTags.Text;
            lib.Description = TextBoxDescription.Text;
            try
            {
                WidgetLibInfoData.Update(lib);
                LabelFinalMessage.Text = "更新成功";
                GridViewWidget.DataSource = ListToArrayList(WidgetLibInfoData.Query());
                GridViewWidget.DataBind();
            }
            catch (DataNotFoundException ex)
            {
                LabelFinalMessage.Text = ex.Message;
            }
            catch (Exception exception)
            {
                LabelFinalMessage.Text = exception.Message;
            }
        }
    }

    public class LibInfo
    {
        public int ID { get; set; }

        public string WidgetName { get; set; }

        public string WidgetFileName { get; set; }

        public string WidgetAssemblyName { get; set; }

        public string Tags { get; set; }

        public string ImageUrl { get; set; }

        public string Description { get; set; }
    }
}
